package web.adminUI;

import java.util.List;

import javax.ejb.EJB;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.RequestScoped;

import web.sharedUI.ErrorMessage;
import web.sharedUI.SessionBean;
import ejb.contentManagement.HotelMgr;
import ejb.dto.HotelDTO;

@ManagedBean
@RequestScoped
public class HotelBean {
	
	public HotelBean() {
		hotel = new HotelDTO();
		setErrorMessage(null);
	}
	
	@EJB
	private HotelMgr hotelMgr; 
	
	@SuppressWarnings("unused")
	private List<HotelDTO> allHotels;
	@SuppressWarnings("unused")
	private List<HotelDTO> allHotelsByLocation;
	private int packageId;
	
	private int hotelId;
	private HotelDTO hotel;
	private String errorMessage;
	
	/**
	 * Delete Hotel from database
	 * @param id
	 * @return redirect to page <code>manageHotels</code>
	 */
	public String deleteHotel(int id) {
		boolean result = hotelMgr.deleteHotel(id);
		if(!result){
			SessionBean.displayError(ErrorMessage._DELETE);
			return null;
		}
		return "/admins/manageHotels?faces-redirect=true";
	}
	
	/**
	 * Update Hotel in database
	 * @return redirect to page <code>manageHotels</code>
	 */
	public String updateHotel() {
		boolean result = hotelMgr.updateHotel(hotel);
		if(!result){
			SessionBean.displayError(ErrorMessage._UPDATE);
			return null;
		}
		return "/admins/manageHotels?faces-redirect=true";
	}
	
	/**
	 * Insert Hotel in database
	 * @return redirect to page <code>manageHotels</code>
	 */
	public String insertHotel() {
		boolean result = hotelMgr.insertHotel(hotel);
		if(!result){
			SessionBean.displayError(ErrorMessage._INSERT);
			return null;
		}
		return "/admins/manageHotels?faces-redirect=true";
	}
	
	/*
	 * GETTERS & SETTERS
	 */
	
	public List<HotelDTO> getAllHotels() {
		return hotelMgr.getAllHotelsDTO();
	}

	public void setAllHotels(List<HotelDTO> allHotels) {
		this.allHotels = allHotels;
	}


	public int getHotelId() {
		return hotelId;
	}

	public void setHotelId(int hotelId) {
		this.hotelId = hotelId;
		hotel = hotelMgr.getHotelDTO(hotelId);
		if(hotel == null) {
			SessionBean.notFound();
		}
	}

	public HotelDTO getHotel() {
		return hotel;
	}

	public void setHotel(HotelDTO hotel) {
		this.hotel = hotel;
	}

	public String getErrorMessage() {
		return errorMessage;
	}

	public void setErrorMessage(String errorMessage) {
		this.errorMessage = errorMessage;
	}

	public List<HotelDTO> getAllHotelsByLocation(int locationId) {
		return hotelMgr.getAllHotelsByLocation(locationId);
	}

	public void setAllHotelsByLocation(List<HotelDTO> allHotelsByLocation) {
		this.allHotelsByLocation = allHotelsByLocation;
	}

	public int getPackageId() {
		return packageId;
	}

	public void setPackageId(int packageId) {
		this.packageId = packageId;
	}	

	
}
